
package UserInterface;

import Main.Tuxedo;
import javax.microedition.lcdui.Display;

/**
 * Main handler for menus. Navigator handles Menu's option movement and serves as a bridge between the MIDlet Tuxedo and Menu and
 * Thread MenuMotion
 * @author Gerardo Galíndez
 */

public class Navigator {

    private Tuxedo midlet;
    private Menu menu;
    private int currentMenu;
    /**
     * Navigator integer index code representing the Main menu.
     */
    protected static final int MAIN = 9;
    /**
     * Navigator integer index code representing the New Game menu.
     */
    protected static final int NEW_GAME = 0;
    /**
     * Navigator integer index code representing the New Game, Choose Character menu.
     */
    protected static final int NEW_GAME_CHARACTER = 10;
    /**
     * Navigator integer index code representing the Continue menu.
     */
    protected static final int CONTINUE = 1;
    /**
     * Navigator integer index code representing the Options menu.
     */
    protected static final int OPTIONS = 2;
    /**
     * Navigator integer index code representing the High Scores menu.
     */
    protected static final int HIGH_SCORES = 3;
    /**
     * Navigator integer index code representing the Help menu.
     */
    protected static final int HELP = 4;
    /**
     * Navigator integer index code representing the Exit menu.
     */
    protected static final int EXIT = 5;

    /**
     * Creates an object contaning a reference to it's container MIDlet and a Menu extending GameCanvas. Navigator auto starts the
     * main menu with the MAIN variable.
     * @param midlet Container MIDlet.
     */
    public Navigator(Tuxedo midlet) {
        this.midlet = midlet;
        menu = new Menu(this);
        this.setCurrentMenu(Navigator.MAIN);
    }

    /**
     * Changes the menu to the selected one.
     * @param newMenu Navigator integer index constant representing the current selected Menu.
     */
    public void setCurrentMenu(int newMenu){
        currentMenu = newMenu;
    }

    /**
     * Sets the MIDlet's display to the GameCanvas Menu.
     * @param currentDisplay Display sent by the MIDlet.
     */
    public void inicialize(Display currentDisplay){
        currentDisplay.setCurrent(menu);
    }

    /**
     * Sets the corresponding difficulty to the game
     * @param difficulty of the game
     */
    public void setDifficulty(char difficulty){
       this.midlet.setDifficulty(difficulty);
    }
    /**
     * In case that destroyApp is called by the OS and not by the user, finalize() takes care of correctly bringing down the whole application.
     * This may happen if destroyApp is called without shutDownApp(). In general, a cellphone exiting the application using an especific
     * handler will call destroyApp, which will call this method to correctly exit the application.
     * For example, in a Sony Ericsson, this may happen if the application is ordered to shut itself down by clicking "Back".
     * @see shutDownApp()
     * @see destroyApp()
     */
    public void finalize() {
        if(menu != null) {
           this.menu.shutDown(true);
        }
    }

    /**
     * Returns the selected Menu at the current time.
     * @return Navigator integer index constant representing the current selected Menu.
     */
    public int getCurrentMenu() {
        return currentMenu;
    }

    /**
     * Executes the shut down sequence. Navigator's shutDown() is called by Menu after starting the game or exiting.
     * @param exitFlag true to call destroyApp and terminate the application, false to destroy only the Navigator and start the game.
     */
    public void shutDown(boolean exitFlag) {
        this.menu = null;
        midlet.shutDownApp(exitFlag);
    }

    /**
     * Calls switchDisplay() at the MIDlet to start the game.
     * @param displayCode Tuxedo integer index constant representing a screen type.
     */
    public void switchDisplay(int displayCode, boolean isNewGame, int file, boolean defaultLanguage) {
        midlet.switchDisplay(displayCode, isNewGame, file, defaultLanguage);
    }

    /**
     * Returns the maximum options a given menu has.
     * @param selectedOption Navigator integer index constant representing the current selected Menu.
     * @return Maximum selectable options for a Menu to choose.
     */
    public int getMaxCurrentOptions(int selectedOption) {
        switch(selectedOption) {
            case NEW_GAME:
                return 3;
            case NEW_GAME_CHARACTER:
                return 3;
            case CONTINUE:
                return 3;
            case OPTIONS:
                return 3;
            case HIGH_SCORES:
                return 1;
            case HELP:
                return 1;
            case EXIT:
                return 2;
            case MAIN:
                return 6;
            default:
                return 0;
        }
    }

    /**
     * Sets the MIDlet's character code in order to create a new game.
     * @param newCharacter Tuxedo integer index code representing a to-be Main Player.
     * @see MainPlayer
     */
    public void setNewGameOptions(int newCharacter) {
        //Needs to be modified, preferently deleted.
        midlet.setCharacterCode(newCharacter);
    }

    public void toggleMusic() {
        menu.toggleMusic();
    }

    public void resumeMusic() {
        menu.resumeMusic();
    }
}